import urllib.request
from userplat.models import jiemaPlat

# 短租类平台登录
def jmdzLogin(url, count, pwd, id):
    try:
        url = url.replace('用户名',count)
        url = url.replace('密码',pwd)
        response = urllib.request.urlopen(url).read()
        token = str(response,encoding='utf-8').split("|")[1]
        print(token)
        if(token != 'to_fast_try_again'):
            return token
    except:
        return False

# 芒果类平台登录
def jmmgLogin(url,count,pwd,id):
    try:
        url = url.replace('API账号', count)
        url = url.replace('密码',pwd)
        response = urllib.request.urlopen(url).read()
        print(str(response,encoding='utf-8'))
        token = str(response,encoding='utf-8').split("|")[1]
        if(token != 'to_fast_try_again'):
            return token
    except:
        return False

# 短租类获取信息
def getdzInfo(jmplats, count, pwd, id):
    for jmplat in jmplats:
        if jmplat.platID == int(id):
            loginUrl = jmplat.loginUrl
            getinfoUrl = jmplat.getinfo
    token = jmdzLogin(loginUrl, count, pwd, id)
    if(token):
        getinfoUrl = getinfoUrl.replace('用户名', count)
        getinfoUrl = getinfoUrl.replace('登录时返回的令牌', token)
        response = urllib.request.urlopen(getinfoUrl)
        try:
            res = str(response.read(), encoding='utf-8').split(";")
            score = res[1]
            money = res[2]
            phnum = res[3]
            return (score, money, phnum)
        except:
            pass
    else:
        pass

# 芒果类获取信息
def getmgInfo(jmplats, count, pwd, id):
    for jmplat in jmplats:
        if jmplat.platID == int(id):
            loginUrl = jmplat.loginUrl
            getinfoUrl = jmplat.getinfo
    token = jmmgLogin(loginUrl, count, pwd, id)
    if(token):
        getinfoUrl = getinfoUrl.replace('用户名', count)
        getinfoUrl = getinfoUrl.replace('登录时返回的令牌', token)
        response = urllib.request.urlopen(getinfoUrl)
        try:
            res = str(response.read(), encoding='utf-8').split("|")
            level = res[1]
            money = res[2]
            phnum = res[3]
            return (level, money, phnum)
        except:
            pass
    else:
        pass

# 易码类获取信息
def getymInfo(jmplats, count, pwd, id):
    for jmplat in jmplats:
        if jmplat.platID == int(id):
            loginUrl = jmplat.loginUrl
            getinfoUrl = jmplat.getinfo
    token = jmdzLogin(loginUrl, count, pwd, id)
    if(token):
        getinfoUrl = getinfoUrl.replace('TOKEN', token)
        response = urllib.request.urlopen(getinfoUrl)
        try:
            res = str(response.read(), encoding='utf-8').split("|")
            level = res[3]
            money = res[4]
            phnum = res[7]
            score = res[5]
            return (level, money, phnum,score)
        except:
            pass
    else:
        pass

# 桔子类获取信息
def getjzInfo(jmplats, count, pwd, id):
    for jmplat in jmplats:
        if jmplat.platID == int(id):
            loginUrl = jmplat.loginUrl
            getinfoUrl = jmplat.getinfo
    token = jmmgLogin(loginUrl, count, pwd, id)
    if(token):
        getinfoUrl = getinfoUrl.replace('登录时返回的令牌', token)
        response = urllib.request.urlopen(getinfoUrl)
        try:
            res = str(response.read(), encoding='utf-8').split("|")
            level = res[1]
            money = res[2]
            phnum = res[3]
            return (level, money, phnum)
        except:
            pass
    else:
        pass
# jmplats是数据库中的所有接码平台的合集，id代表要查询第几个接码平台
def mains(jmplats,jmUserInfo,id):
    for jmplat in jmplats:
        if jmplat.platID == id:
            platName = jmplat.platName
            loginUrl = jmplat.loginUrl
            getPhoneUrl = jmplat.getPhoneUrl
            getCodeUrl = jmplat.getCodeUrl
            getinfo = jmplat.getinfo
    UserCount = jmUserInfo.jiemaCount
    UserPwd = jmUserInfo.jiemaPwd
    token = jmdzLogin(loginUrl, UserCount, UserPwd)
    score, money, phnum = getdzInfo(getinfo, UserCount, token)



# if __name__ == '__main__':
#     main()